package ypmits.sound {
	import ypmits.setup.EasySetup;
	import ypmits.sound.totest.PitchShift;
	import ypmits.utils.Aligner;

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.media.Sound;
	import flash.net.URLRequest;

	/**
	 * @author Dave Lenz
	 */
	public class SoundGraphic extends Sprite implements ISoundGraphic {

		private var _pitchshift : PitchShift;
		private var _mp3 : Sound;
		private var _sound : Sound;
		private var musicswitch : Boolean = true;
		private var soundspeaker : DisplayObject;
		private var soundspeakeroff : DisplayObject;
		private var started : Boolean = false;
		private var stagealign : String;
		private var offset : Point;
		private var autoplay : Boolean;
		private var soundskin : SoundSkin;
		private var musicurl : String;

		
		public function SoundGraphic( soundskin : SoundSkin, musicurl : String, stagealign : String, offset : Point, autoplay : Boolean ) {
			if( !soundskin ) soundskin = new SoundSkin( );
			
			this.soundskin = soundskin;
			this.musicurl = musicurl;
			this.stagealign = stagealign;
			this.offset = offset;
			this.autoplay = autoplay;
			
			draw( );
			
			load( );
		}

		
		private function draw() : void {
			soundspeaker = soundskin.onGraphic;
			soundspeakeroff = soundskin.offGraphic;
			
			addChild( soundspeaker );
			addChild( soundspeakeroff );

			buttonMode = true;
			mouseChildren = false;
			mouseEnabled = true;
			addEventListener( MouseEvent.CLICK, mouseHandler );
			
			EasySetup.instance.stage.addEventListener( Event.RESIZE, resizer );
			
			resizer( );
		}

		
		private function load( ) : void {
			_mp3 = new Sound( );
			_mp3.addEventListener( Event.COMPLETE, mp3LoadCompleteHandler );
			_mp3.load( new URLRequest( musicurl ) );

			_sound = new Sound( );
			
			_pitchshift = new PitchShift( _mp3 );
			_pitchshift.rate = 0;


			if( autoplay ) {
				soundspeaker.visible = true;
				soundspeakeroff.visible = false;
				
				play();
				
				_pitchshift.play( );
				
				started = true;
			} else {
				soundspeaker.visible = false;
				soundspeakeroff.visible = true;
			}
		}

		
		private function mp3LoadCompleteHandler(event : Event) : void {
		}

		
		private function mouseHandler( e : MouseEvent ) : void {
			if( musicswitch ) {
				musicswitch = false;
				soundspeaker.visible = false;
				soundspeakeroff.visible = true;
				
				pause( );
			} else {
				if( started ) {
					musicswitch = true;
					soundspeaker.visible = true;
					soundspeakeroff.visible = false;
					
					play( );
				} else {
					_pitchshift.play( );
					started = true;
				}
			}
		}

		
		/**
		 * You can override this function with a tweener to 
		 */
		public function setVolume( volume : Number ) : void {
			trace( "Error! SoundGraphic.setVolume needs to be overridden! Check the 'pauseMusic()' when the setVolume( " + volume + " ) is done!" );
			//_pitchshift.volume = volume;
		}

		
		public function pause() : void {
			_pitchshift.rate = 0;
		}

		
		public function play() : void {
			_pitchshift.rate = 1.5;
		}

		
		private function resizer( e : Event = null ) : void {
			Aligner.align( this, null, stagealign, offset, EasySetup.instance.stage );
		}
	}
}
